/*
 * Resources.java
 *
 * Created on September 15, 2007, 5:35 PM
 */

package picto.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * A simple utility class for accessing program resources such as files or images.
 * @author davedes
 */
public class Resources {
        
    protected Resources() {
    }
    
    /**
     * Convenience method for retrieving a String from the specified bundle.
     * If the string is not found or if the bundle is <tt>null</tt>, the passed key is returned.
     * @param bundle the bundle to use for lookup
     * @param key the key to lookup
     * @return the value of the key
     */
    public static String getString(ResourceBundle bundle, String key, String def) {
        if (bundle!=null) {
            try {
                return bundle.getString(key);
            } catch (MissingResourceException e) {
                return def;
            }
        } else {
            return def;
        }
    }
    
    /**
     * Convenience method for retrieving a String from the specified bundle.
     * If the string is not found or if the bundle is <tt>null</tt>, the passed key is returned.
     * @param bundle the bundle to use for lookup
     * @param key the key to lookup
     * @return the value of the key
     */
    public static String getString(ResourceBundle bundle, String key) {
        return getString(bundle, key, key);
    }
    
    /**
     * Opens and returns an InputStream for the specified resource from the classpath. 
     * If the resource isn't found, the method will use the local File system 
     * as a fallback (mostly only useful for testing purposes).
     * @param ref the resource to find
     * @return the InputStream of the resource or <tt>null</tt> if it cannot be found
     */
    public static InputStream getResourceAsStream(String ref) {
        String cref = ref.replace('\\', '/');
        InputStream in = Resources.class.getClassLoader().getResourceAsStream(cref);
        if (in==null) {
            File f = new File(cref);
            try {
                in = new FileInputStream(f);
                return new BufferedInputStream(in);
            } catch (Exception e) {
                in = null;
            }
        }
        return in;
    }
    
    /**
     * Returns a URL pointing to the specified resource from the classpath. 
     * If the resource isn't found, the method will use the local File system 
     * as a fallback (mostly only useful for testing purposes).
     * @param ref the resource to find
     * @return the InputStream of the resource or <tt>null</tt> if it cannot be found
     */
    public static URL getResource(String ref) {
        String cref = ref.replace('\\', '/');
        URL in = Resources.class.getClassLoader().getResource(cref);
        if (in==null) {
            File f = new File(cref);
            if (f.exists()) {
                try { return f.toURI().toURL(); }
                catch (MalformedURLException e) {}
            }
        }
        return in;
    }
}
